/* * MainFrame.java
 *
 * Created on 24/10/2010, 19:28:11
 */
package clinicapoo.view.main;

import clinicapoo.factory.DAO;
import clinicapoo.view.dialog.Dialogs;
import clinicapoo.view.especialidade.CadastroEspecialidadePanel;
import clinicapoo.view.medic.CadastroMedicoPanel;
import clinicapoo.view.medic.ListaMedicosPanel;

import clinicapoo.view.paciente.frmCadastrarPaciente;
import clinicapoo.view.paciente.ListaPacientePanel;
import clinicapoo.view.paciente.frmAlterar;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JDesktopPane;

/** *
 * @author fmontezuma
 */
public class MainFrame extends javax.swing.JFrame
{
	private static MainFrame instance;

	JDesktopPane desktop;
	private Map<Class, Component> janelasAbertas = new HashMap<Class, Component>();

	/** Creates new form MainFrame */
	public MainFrame()
	{
		initComponents();
		desktop = new JDesktopPane();
		setContentPane(desktop);
		//Make dragging a little faster but perhaps uglier.
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        menu_especCadastrar = new javax.swing.JMenuItem();

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("resources/messages"); // NOI18N
        setTitle(bundle.getString("view.main.title")); // NOI18N

        jMenu1.setText(bundle.getString("view.main.menu.medicos")); // NOI18N

        jMenuItem1.setText(bundle.getString("view.main.menu.medicos.cadastrar")); // NOI18N
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem1MouseClicked(evt);
            }
        });
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem3.setText(bundle.getString("view.main.menu.medicos.listar")); // NOI18N
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText(bundle.getString("view.main.menu.pacientes")); // NOI18N

        jMenuItem2.setText(bundle.getString("view.main.menu.pacientes.cadastrar")); // NOI18N
        jMenuItem2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem2MouseClicked(evt);
            }
        });
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem4.setText(bundle.getString("view.main.menu.pacientes.listar")); // NOI18N
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuBar1.add(jMenu2);

        jMenu5.setText("Agendamento");

        jMenuItem5.setText("consultar");
        jMenu5.add(jMenuItem5);

        jMenuBar1.add(jMenu5);

        jMenu6.setText("Atualizar Dados");

        jMenuItem6.setText("Pacientes");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem6);

        jMenuItem7.setText("Médicos");
        jMenu6.add(jMenuItem7);

        jMenuBar1.add(jMenu6);

        jMenu7.setText("Especialidade");

        menu_especCadastrar.setText("Cadastrar");
        menu_especCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_especCadastrarActionPerformed(evt);
            }
        });
        jMenu7.add(menu_especCadastrar);

        jMenuBar1.add(jMenu7);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 927, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 545, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
		abrirJanela(CadastroMedicoPanel.class);
	}//GEN-LAST:event_jMenuItem1ActionPerformed

        private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MouseClicked
			// TODO add your handling code here:
        }//GEN-LAST:event_jMenuItem1MouseClicked

        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
			abrirJanela(frmCadastrarPaciente.class);
        }//GEN-LAST:event_jMenuItem2ActionPerformed

        private void jMenuItem2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem2MouseClicked
        }//GEN-LAST:event_jMenuItem2MouseClicked

		private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItem3ActionPerformed
		{//GEN-HEADEREND:event_jMenuItem3ActionPerformed
			abrirJanela(ListaMedicosPanel.class);
		}//GEN-LAST:event_jMenuItem3ActionPerformed

                private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
					abrirJanela(ListaPacientePanel.class);
                }//GEN-LAST:event_jMenuItem4ActionPerformed

                private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
					abrirJanela(frmAlterar.class);
                }//GEN-LAST:event_jMenuItem6ActionPerformed

				private void menu_especCadastrarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_menu_especCadastrarActionPerformed
				{//GEN-HEADEREND:event_menu_especCadastrarActionPerformed
					abrirJanela(CadastroEspecialidadePanel.class);
				}//GEN-LAST:event_menu_especCadastrarActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[])
	{
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				DAO.startEntityManager();
				instance = new MainFrame();
				instance.setVisible(true);
			}
		});
	}

	private void abrirJanela(Class c)
	{
		try
		{
			if (!janelasAbertas.containsKey(c))
			{
				Component janela = (Component) c.newInstance();
				janelasAbertas.put(c, janela);
				janela.setVisible(true);
				desktop.add(janela);
			}
			else
			{
				Component janela = janelasAbertas.get(c);
				if (!janela.isVisible())
				{
					janelasAbertas.remove(c);
					abrirJanela(c);
				}
			}
		}
		catch (Exception e)
		{
			Dialogs.showError(this, e);
		}
	}

	public static MainFrame getInstance()
	{
		return instance;
	}

	public Map<Class, Component> getJanelasAbertas()
	{
		return janelasAbertas;
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem menu_especCadastrar;
    // End of variables declaration//GEN-END:variables
}
